package com.mall.controller;

import com.mall.common.resp.ResponseResult;
import com.mall.entity.Favorite;
import com.mall.entity.Goods;
import com.mall.service.FavoriteService;
import com.mall.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xyl
 * @date 2021-03-02 14:50
 */
@Api(tags = "收藏接口")
@RestController
@RequestMapping("favorite")
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private GoodsService goodsService;

    @ApiOperation("添加收藏")
    @PostMapping("add")
    public ResponseResult add(@RequestParam Integer goodsId){
        Favorite favorite = favoriteService.getFavoriteByGoodsId(goodsId);
        if (ObjectUtils.isNotEmpty(favorite)) {
            return ResponseResult.error("商品已被收藏!");
        }
        return ResponseResult.toResponse(favoriteService.add(goodsId));
    }

    @ApiOperation("移除收藏  goodsId")
    @PostMapping("remove")
    public ResponseResult remove(@RequestParam Integer goodsId){
        return ResponseResult.toResponse(favoriteService.remove(goodsId));
    }

    @ApiOperation("获取当前用户收藏")
    @GetMapping("get")
    public List<Goods> get(){
        return goodsService.getGoodsByFavorite();
    }
}
